/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.peng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.peng.domain.RecyclerAddress;
import me.peng.domain.config.SysResult;
import me.peng.domain.mapper.UserAddressMapper;
import me.peng.utils.ValidationUtil;
import me.peng.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.peng.repository.RecyclerAddressRepository;
import me.peng.service.RecyclerAddressService;
import me.peng.service.dto.RecyclerAddressDto;
import me.peng.service.dto.RecyclerAddressQueryCriteria;
import me.peng.service.mapstruct.RecyclerAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.peng.utils.PageUtil;
import me.peng.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author pengjianye
* @date 2021-09-30
**/
@Service
@RequiredArgsConstructor
public class RecyclerAddressServiceImpl implements RecyclerAddressService {

    private final RecyclerAddressRepository recyclerAddressRepository;
    private final RecyclerAddressMapper recyclerAddressMapper;
    @Autowired
    UserAddressMapper userAddressMapper;

    @Override
    public Map<String, Object> queryAll(RecyclerAddressQueryCriteria criteria, Pageable pageable) {
        Page<RecyclerAddress> page = recyclerAddressRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(recyclerAddressMapper::toDto));
    }

    @Override
    public List<RecyclerAddressDto> queryAll(RecyclerAddressQueryCriteria criteria) {
        return recyclerAddressMapper.toDto(recyclerAddressRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public RecyclerAddressDto findById(Integer id) {
        RecyclerAddress recyclerAddress = recyclerAddressRepository.findById(id).orElseGet(RecyclerAddress::new);
        ValidationUtil.isNull(recyclerAddress.getId(), "RecyclerAddress", "id", id);
        return recyclerAddressMapper.toDto(recyclerAddress);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RecyclerAddressDto create(RecyclerAddress resources) {
        return recyclerAddressMapper.toDto(recyclerAddressRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RecyclerAddress resources) {
        RecyclerAddress recyclerAddress = recyclerAddressRepository.findById(resources.getId()).orElseGet(RecyclerAddress::new);
        ValidationUtil.isNull(recyclerAddress.getId(), "RecyclerAddress", "id", resources.getId());
        recyclerAddress.copy(resources);
        recyclerAddressRepository.save(recyclerAddress);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            recyclerAddressRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<RecyclerAddressDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RecyclerAddressDto recyclerAddress : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("关联编号", recyclerAddress.getParentId());
            map.put("收货人", recyclerAddress.getReceiver());
            map.put("收货号码", recyclerAddress.getPhone());
            map.put("地址", recyclerAddress.getAddress());
            map.put("详细地址", recyclerAddress.getDetaiAddress());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


    //post  添加地址信息
    @Override
    public SysResult addressUser(RecyclerAddress recyclerAddress) {

        userAddressMapper.insert(recyclerAddress);
        return SysResult.success();
    }

    //GET  根据父ID查询当前用户的所有地址数据
    @Override
    public SysResult selectAddressUser(Integer parentId) {

        QueryWrapper<RecyclerAddress> queryWrapper = new QueryWrapper<>();
        //selec * from recycler_address where parent_id=pareiId
        queryWrapper.eq("parent_id", parentId);
        List<RecyclerAddress> list = userAddressMapper.selectList(queryWrapper);

        return SysResult.success(list);
    }


    //修改地址信息
    @Override
    public SysResult updateAddressInfo(RecyclerAddress user) {
        userAddressMapper.updateById(user);
        return SysResult.success();
    }

    //删除地址
    @Override
    public SysResult deleteAddressInfo(Integer id) {
        userAddressMapper.deleteById(id);
        return SysResult.success();
    }

    //根据地址id查询
    @Override
    public SysResult selectIdAddress(Integer id) {
        RecyclerAddress recyclerAddress = userAddressMapper.selectById(id);
        return SysResult.success(recyclerAddress);
    }

}

